import { Component, OnInit } from '@angular/core';
import { ListService, PagedResultDto } from '@abp/ng.core';
import { RfidService } from './rfid.service';
import { CreateUpdateRfidDto, RfidDto } from './models';
import { CommonModule } from '@angular/common';
import { ThemeSharedModule, ConfirmationService, Confirmation, ToasterService } from '@abp/ng.theme.shared';
import { NgxDatatableModule } from '@swimlane/ngx-datatable';
import { NgbModal } from '@ng-bootstrap/ng-bootstrap';
import { RfidModalComponent } from './rfid-modal.component';

@Component({
  selector: 'app-rfids',
  standalone: true,
  imports: [CommonModule, ThemeSharedModule, NgxDatatableModule],
  templateUrl: './rfids.component.html',
  styleUrls: [],
  providers: [ListService],
})
export class RfidsComponent implements OnInit {
  rfid = { items: [], totalCount: 0 } as PagedResultDto<RfidDto>;

  constructor(
    public readonly list: ListService, 
    private rfidService: RfidService,
    private modalService: NgbModal,
    private confirmation: ConfirmationService,
    private toaster: ToasterService
  ) {}

  ngOnInit() {
    this.list.page = 1;
    const rfidStreamCreator = (query) => this.rfidService.getList(query);

    this.list.hookToQuery(rfidStreamCreator).subscribe((response) => {
      this.rfid = response;
    });
  }

  create() {
    const modalRef = this.modalService.open(RfidModalComponent);
    modalRef.result.then((result: CreateUpdateRfidDto) => {
      this.rfidService.create(result).subscribe(() => {
        this.list.get();
        this.toaster.success('成功创建RFID');
      });
    });
  }

  edit(row: RfidDto) {
    const modalRef = this.modalService.open(RfidModalComponent);
    modalRef.componentInstance.setRfid(row);
    modalRef.result.then((result: CreateUpdateRfidDto) => {
      this.rfidService.update(row.id, result).subscribe(() => {
        this.list.get();
        this.toaster.success('成功更新RFID');
      });
    });
  }

  delete(row: RfidDto) {
    this.confirmation.warn('您确定要删除这个RFID吗？', '确认删除').subscribe((status) => {
      if (status === Confirmation.Status.confirm) {
        this.rfidService.delete(row.id).subscribe(() => {
          this.list.get();
          this.toaster.success('成功删除RFID');
        });
      }
    });
  }
} 